// $Id: loggerorphanfiles.cpp 34 2004-09-22 13:07:59Z hansemil $
#include "loggerorphanfiles.h"
#include "loghandler.h"
#include "filehandler.h"
#include "file.h"
#include "node.h"
#include <iostream>
#include <list>
#include <vector>
using namespace std;

/**
 * @todo Write documentation
 */
LoggerOrphanFiles::LoggerOrphanFiles()
{
	LogHandler::getInstance()->addLogger(this, "clock_tick");
}

/**
 * Opens file for writing and stores the Eos pointer
 */
void LoggerOrphanFiles::eos_simulate_start(void *eos)
{
	openFile(&m_fout, "orphanfiles.csv");
	m_fout << "Time Step, Orphan File Count, Total File Count" << endl;

	m_eos = (Eos *)eos;
}

/**
 * Closes file
 */
void LoggerOrphanFiles::eos_simulate_stop(void *eos)
{
	m_fout.close();
}

/**
 * Generates and writes the statistics for each tick
 */
void LoggerOrphanFiles::clock_tick(unsigned int time)
{
	std::list<Node *> *nodes;
	int orphanFileCount = 0, i;
	bool orphanFile;
	File *file;

	// Write time step
	m_fout << time;

	// Collect statistics
	nodes = m_eos->getNodes();
	for(i=0;(file = FileHandler::getFileByIndex(i));i++){
		orphanFile = true;
		for (std::list<Node *>::iterator node=nodes->begin(); node!=nodes->end() && orphanFile; node++) {
			if((*node)->getStoredFile(file)){
				orphanFile = false;
			}
		}
		if(orphanFile){
			orphanFileCount++;
		}
	}

	// Write statistics
	m_fout << "," << orphanFileCount;
	m_fout << "," << i;

	// Write end
	m_fout << endl;

}
